﻿Imports BLL
Imports EntitiesLayer

Public Class FrmConsultarMaquina

    Public lista As List(Of Maquina)
    Public maquinaXModificar As New Maquina

    Private Sub FrmConsultarMaquina_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        lista = objGestor.listarMaquinas()

        tblMaquinas.AutoGenerateColumns = False
        tblMaquinas.DataSource = lista

        tblMaquinas.Columns("id").DataPropertyName = "id"
        tblMaquinas.Columns("nombre").DataPropertyName = "nombre"
        tblMaquinas.Columns("numero_maquina").DataPropertyName = "numero_maquina"
        tblMaquinas.Columns("numero_activo").DataPropertyName = "numero_activo"
        tblMaquinas.Columns("estado").DataPropertyName = "estado"
    End Sub

    Private Sub btnInfoMaquina_Click(sender As Object, e As EventArgs) Handles btnInfoMaquina.Click
        Dim idMaquina As Integer
        Dim frm As New FrmModificarMaquina()

        idMaquina = tblMaquinas.CurrentRow.Cells(0).Value
        maquinaXModificar = objGestor.buscarMaquina(idMaquina)

        frm.txtId.Text = idMaquina
        frm.txtNombre.Text = maquinaXModificar.Nombre
        frm.txtNumMaquina.Text = maquinaXModificar.NumMaquina
        frm.txtNumActivo.Text = maquinaXModificar.NumActivo
        If maquinaXModificar.Estado Then
            frm.cmbEstado.SelectedIndex = 0
        Else
            frm.cmbEstado.SelectedIndex = 1
        End If

        frmTransparente.AgregarContenido(frm)
        frm.Dock = DockStyle.Fill
        frmTransparente.Show()
    End Sub

    Private Sub btnRegistrarMaquina_Click(sender As Object, e As EventArgs) Handles btnRegistrarMaquina.Click
        Dim frm As New Control
        frm = New FrmRegistrarMaquina
        frmTransparente.AgregarContenido(frm)
        frm.Dock = DockStyle.Fill
        frmTransparente.Show()
    End Sub

    Private Sub txtFiltrarTabla_TextChanged(sender As Object, e As EventArgs) Handles txtFiltrarTabla.TextChanged
        Dim listaFiltrada = New List(Of Maquina)()
        Dim filtro = txtFiltrarTabla.Text.ToUpper()
        If filtro Is String.Empty Then
            listaFiltrada = lista
        Else
            For Each maquina As Maquina In lista
                If maquina.Nombre.ToUpper().Contains(filtro) Or maquina.NumMaquina.ToUpper().Contains(filtro) Or
                    maquina.NumActivo.ToUpper().Contains(filtro) Then
                    listaFiltrada.Add(maquina)
                End If
            Next
        End If
        tblMaquinas.DataSource = listaFiltrada
    End Sub

End Class
